package com.kamistoat.meimeimall.meimeimallproduct.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillSkuInfoTo;
import com.kamistoat.meimeimall.common.To.productTo.SkuBaseInfoTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.dubboInterface.SearchDubboInterface;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.meimeimallproduct.dao.SkuSaleAttrValueDao;
import com.kamistoat.meimeimall.meimeimallproduct.service.BrandService;
import com.kamistoat.meimeimall.meimeimallproduct.service.SpuInfoService;
import com.kamistoat.meimeimall.meimeimallproduct.vo.SkuUpdateVo;
import com.kamistoat.meimeimall.meimeimallproduct.entity.SkuInfoEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeimall.meimeimallproduct.service.SkuInfoService;
import com.kamistoat.meimeimall.common.utils.R;


/**
 * sku信息
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@Api(tags = "sku信息数据库操作")
@RestController
@RequestMapping("skuinfo")
public class SkuInfoController {
    protected Logger logger = LoggerFactory.getLogger(SkuInfoController.class);

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SkuSaleAttrValueDao skuSaleAttrValueDao;
    @Autowired
    BrandService brandService;

    @Autowired
    SearchDubboInterface searchDubboInterface;

    /**
     * 列表
     */
    @ApiOperation("从数据库获取列表")
    @GetMapping("/list")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPageBySkuInfo(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("从数据查询指定POJO")
    @GetMapping("/info/{skuId}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:skuinfo:info")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/info/webupdate")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R webUpdate(@RequestBody SkuUpdateVo skuUpdateVo) {
        if (skuUpdateVo.getSkuId() == -1) {
            return R.error(1, "异常错误，请重试");
        }
        SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
        BeanUtils.copyProperties(skuUpdateVo, skuInfoEntity);
        boolean sqlStatus = skuInfoService.updateById(skuInfoEntity);
        if (!sqlStatus) {
            return R.error(1, "sku修改失败请重试");
        } else {
            skuInfoService.skuUpToES(skuUpdateVo.getSkuId());
            return R.ok();
        }
    }

    /**
     * 对单个sku商品修改ES
     *
     * @param skuId
     * @return
     */
    @ApiOperation("将指定sku上架到ES中")
    @PostMapping("/sku_up_es/{skuId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R skuUpToES(@PathVariable("skuId") Long skuId) {
        skuInfoService.skuUpToES(skuId);
        return R.ok();
    }

    /**
     * 对单个sku商品删除ES
     *
     * @param skuId
     * @return
     */
    @ApiOperation("将指定sku从ES中下架")
    @DeleteMapping("/sku_delete_es/{skuId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R skuDeleteToES(@PathVariable("skuId") Long skuId) {
        List<Long> skuIdList = Collections.singletonList(skuId);
        return searchDubboInterface.skuDownToES(Collections.singletonList(skuId));
    }


    /**
     * 查询指定skuId的简略信息
     *
     * @param skuId skuId
     * @return SkuBaseInfo To类
     */
    @ApiOperation("查询指定skuId信息")
    @GetMapping("/sku_base_info/{skuId}")
    @DubboAndHTTPExceptionCatch
    public SkuBaseInfoTo getSkuBaseInfo(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
        SkuBaseInfoTo skuBaseInfo = new SkuBaseInfoTo();
        BeanUtils.copyProperties(skuInfoEntity, skuBaseInfo);
        return skuBaseInfo;
    }

    /**
     * 查询指定skuId的秒杀信息
     */
    @ApiOperation("查询指定skuId的秒杀信息")
    @GetMapping("/sku_seckill_info/{skuId}")
    @DubboAndHTTPExceptionCatch
    public SecKillSkuInfoTo getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
        SecKillSkuInfoTo secKillSkuInfoTo = new SecKillSkuInfoTo();
        BeanUtils.copyProperties(skuInfoEntity, secKillSkuInfoTo);
        // 一并查询销售属性
        secKillSkuInfoTo.setSkuAttr(skuSaleAttrValueDao.getStringAttr(skuId));
        // 一并查商品品牌名称
        secKillSkuInfoTo.setBrandName(brandService.getById(secKillSkuInfoTo.getBrandId()).getName());
        return secKillSkuInfoTo;
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/sku_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfoEntity) {
        skuInfoService.save(skuInfoEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("向数据库更新POJO")
    @PostMapping("/sku_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/sku_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
